# koroFileHeader at Yungoal acer
# Create: 2023-10-18 13:56:57
# LastEdit: 2023-10-20 14:39:02
"""说明暂无"""
__author__ = '749B'
# pylint: disable=missing-class-docstring, missing-function-docstring
# pylint: disable=invalid-str-returned

from alibabacloud_resourcemanager20200331.client import Client as ResourceManager20200331Client
from alibabacloud_tea_openapi import models as open_api_models

from django.contrib import admin
from django.db import models

from utils.vault import decrypt, encrypt     # pylint: disable=import-error
from secret.encrypt import ENCRYPT_PASSWOED  # pylint: disable=import-error


class Customer(models.Model):
    name = models.CharField(max_length=32, unique=True, verbose_name="客户名称")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '公司'
        verbose_name_plural = '公司'


class CustomerAdmin(admin.ModelAdmin):
    pass


class Account(models.Model):
    name = models.CharField(max_length=32, unique=True, verbose_name="账号名称")
    account_alias = models.CharField(max_length=32, unique=True, verbose_name="企业别名")
    account_id = models.CharField(max_length=32, unique=True, verbose_name="主账号ID")

    customer = models.ForeignKey('Customer', on_delete=models.CASCADE, verbose_name='客户名称')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '账号'
        verbose_name_plural = '账号'

class AccountAdmin(admin.ModelAdmin):
    list_display = ['name', 'account_alias', 'account_id', 'customer']


class User(models.Model):
    name = models.CharField(max_length=32, unique=True, verbose_name="用户名称")
    access_key_id = models.CharField(max_length=32, unique=True)
    access_key_secret = models.TextField(max_length=512)
    created_at = models.DateTimeField(blank=True, null=True, verbose_name="创建时间")
    status = models.BooleanField(default=True)

    account = models.ForeignKey('Account', on_delete=models.CASCADE, verbose_name='账号名称')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = '用户'

    def save(self, *args, **kwargs):
        """重写save方法,在save之前对secret加密"""
        self.access_key_secret = encrypt(self.access_key_secret, ENCRYPT_PASSWOED)
        super().save(*args, **kwargs)

    @property
    def rm_client(self):
        access_key_id = self.access_key_id
        access_key_secret = decrypt(self.access_key_secret, ENCRYPT_PASSWOED)
        config = open_api_models.Config(access_key_id, access_key_secret)
        config.endpoint = 'resourcemanager.aliyuncs.com'
        return ResourceManager20200331Client(config)


class UserAdmin(admin.ModelAdmin):
    list_display = ['name', 'access_key_id', 'status', 'created_at', 'account']
    fieldsets = (
        (None, {
            'fields': ('name', 'account', 'status')
        }),
        ('Access Key', {
            'classes': ('collapse',),
            'fields': ('access_key_id', 'access_key_secret', 'created_at')
        })
    )
